package com.yanqu.road.dao.impl.palace;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.decoration.DecorationSimple;
import com.yanqu.road.entity.estatewar.palace.PalaceFloorInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PalaceFloorInfoDaoImpl extends TempDataObjectDao<PalaceFloorInfo> {

    public Map<Integer, PalaceFloorInfo> getPalaceFloorInfoMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PalaceFloorInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_palace_floor where activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PalaceFloorInfo data = parse(rs);
                dataMap.put(data.getPos(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(PalaceFloorInfo data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_palace_floor(activityId,pos,userId,totalAbility,modifyTime,decorationData) " +
                "values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getPos());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getTotalAbility().toString());
            call.setLong(i++, data.getModifyTime());
            call.setString(i++, data.getDecorationSimpleListJsonStr());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(PalaceFloorInfo data) {
        PreparedStatement call = null;
        String sql = "update t_c_palace_floor set userId=?,totalAbility=?,modifyTime=?,decorationData=?" +
                " where activityId=? and pos=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getTotalAbility().toString());
            call.setLong(i++, data.getModifyTime());
            call.setString(i++, data.getDecorationSimpleListJsonStr());
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getPos());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected PalaceFloorInfo parseData(ResultSet rs) throws SQLException {
        PalaceFloorInfo data = new PalaceFloorInfo();
        data.setActivityId(rs.getInt("activityId"));
        data.setPos(rs.getInt("pos"));
        data.setUserId(rs.getLong("userId"));
        data.setTotalAbility(new BigInteger(rs.getString("totalAbility")));
        data.setModifyTime(rs.getLong("modifyTime"));
        String param = rs.getString("decorationData");
        if(null == param){
            param = "[]";
        }
        List<DecorationSimple> dataList = JSON.parseObject(param, new TypeReference<List<DecorationSimple>>() {});
        if(null != dataList) {
            data.setDecorationSimpleList(dataList);
        }
        return data;
    }
}
